package com.xwaf.platform.web;

import java.util.Locale;
import java.util.TimeZone;

import javax.annotation.PostConstruct;
import javax.servlet.MultipartConfigElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import com.xwaf.platform.common.web.config.Settings;
import com.xwaf.platform.system.api.entity.auth.Company;
import com.xwaf.platform.system.api.service.ELProjectService;
import com.xwaf.platform.system.api.service.auth.CompanyService;
import com.xwaf.platform.web.config.FilterConfig;

/**
 * 
 * <p>
 * 启动类
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019年12月30日 上午9:40:38 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
@ImportResource("classpath:dubbo-*consumer.xml")
@Import({ Settings.class, FilterConfig.class })
@ComponentScan(basePackages = { "com.xwaf" })
@EnableWebSocket
@ServletComponentScan
// @MapperScan
@EnableCaching
public class WebApplication {

	private static final Logger LOGGER = LoggerFactory.getLogger(WebApplication.class);

	@Autowired
	ELProjectService projectService;

	@Autowired
	private CompanyService companyService;

	@RequestMapping("x/limit")
	public String limit() {
		// dubbo 熔断测试
		Company company = companyService.selectLimit();
		System.out.println(company.getEmail());
		return "limit success";
	}

	/**
	 * <p>
	 * Hello string.
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:40:48 <br>
	 * @see 无<br>
	 * @return the string
	 * @demo <br>
	 */
	@RequestMapping("/x/hello")
	public String hello() {
		Company company1 = companyService.selectById("230");
		LOGGER.info("/x/hello1:" + company1.getEmail());
		Company company2 = companyService.findByOne("230");
		LOGGER.info("/x/hello2:" + company2.getEmail());
		return "Hello World!";
	}

	// 健康检查
	@RequestMapping("/check")
	public String check() {
		return "success";
	}

	@PostConstruct
	void started() {
		TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
	}

	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory config = new MultipartConfigFactory();
		config.setMaxFileSize("60MB");
		config.setMaxRequestSize("100MB");
		return config.createMultipartConfig();
	}

	@Bean
	public LocaleResolver localeResolver() {
		// SessionLocaleResolver resolver = new SessionLocaleResolver();
		// resolver.setDefaultLocale(Locale.US);
		CookieLocaleResolver slr = new CookieLocaleResolver();
		slr.setDefaultLocale(Locale.ENGLISH);
		slr.setCookieMaxAge(3600);
		slr.setCookieName("Language");
		return slr;
	}

	/**
	 * <p>
	 * The entry point of application.
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:41:04 <br>
	 * @see 无<br>
	 * @param args
	 *            the input arguments
	 * @demo <br>
	 */
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(WebApplication.class);
		application.setBannerMode(Banner.Mode.OFF);
		application.run(args);
		LOGGER.info("platform-web started!!!");
	}
}
